/*---------------------------------------------------------------------
 * File name: hal_timer.cpp
 *
 * Copyright (c) <2020-2022>, <ChenLong>
 *
 * All rights reserved.
 *
 * Author: ChenLong
 * Email: worldlong@foxmail.com
 *--------------------------------------------------------------------*/
#include "gd32f30x.h"
#include "hal_system.h"
#include "hal_config.h"
/*
*/
void HAL_System::init()
{
  //AF power control  
#if EXTI_ENABLE
  rcu_periph_clock_enable(RCU_AF);
#endif
  
  //DMA power control  
#if DMA0_ENABLE
  rcu_periph_clock_enable(RCU_DMA0);
#endif
#if DMA1_ENABLE
  rcu_periph_clock_enable(RCU_DMA1);
#endif
  
  //GPIO power control  
#if GPIOA_ENABLE
  rcu_periph_clock_enable(RCU_GPIOA);
#endif
#if GPIOB_ENABLE
  rcu_periph_clock_enable(RCU_GPIOB);
#endif
#if GPIOC_ENABLE
  rcu_periph_clock_enable(RCU_GPIOC);
#endif
#if GPIOD_ENABLE
  rcu_periph_clock_enable(RCU_GPIOD);
#endif
#if GPIOE_ENABLE
  rcu_periph_clock_enable(RCU_GPIOE);
#endif
#if GPIOF_ENABLE
  rcu_periph_clock_enable(RCU_GPIOF);
#endif
#if GPIOG_ENABLE
  rcu_periph_clock_enable(RCU_GPIOG);
#endif
#if GPIOH_ENABLE
  rcu_periph_clock_enable(RCU_GPIOH);
#endif

//TIM power control
#if TIM0_ENABLE
  rcu_periph_clock_enable(RCU_TIMER0);
#endif
#if TIM1_ENABLE
  rcu_periph_clock_enable(RCU_TIMER1);
#endif
#if TIM2_ENABLE
  rcu_periph_clock_enable(RCU_TIMER2);
#endif
#if TIM3_ENABLE
  rcu_periph_clock_enable(RCU_TIMER3);
#endif
#if TIM4_ENABLE
  rcu_periph_clock_enable(RCU_TIMER4);
#endif
#if TIM5_ENABLE
  rcu_periph_clock_enable(RCU_TIMER5);
#endif
#if TIM6_ENABLE
  rcu_periph_clock_enable(RCU_TIMER6);
#endif
#if TIM7_ENABLE
  rcu_periph_clock_enable(RCU_TIMER7);
#endif
#if TIM8_ENABLE
  rcu_periph_clock_enable(RCU_TIMER8);
#endif
#if TIM10_ENABLE
  rcu_periph_clock_enable(RCU_TIMER10);
#endif
#if TIM11_ENABLE
  rcu_periph_clock_enable(RCU_TIMER11);
#endif
#if TIM12_ENABLE
  rcu_periph_clock_enable(RCU_TIMER12);
#endif
#if TIM13_ENABLE
  rcu_periph_clock_enable(RCU_TIMER13);
#endif
#if TIM14_ENABLE
  rcu_periph_clock_enable(RCU_TIMER14);
#endif
#if TIM15_ENABLE
  rcu_periph_clock_enable(RCU_TIMER15);
#endif
#if TIM16_ENABLE
  rcu_periph_clock_enable(RCU_TIMER16);
#endif
#if TIM17_ENABLE
  rcu_periph_clock_enable(RCU_TIMER17);
#endif
#if TIM18_ENABLE
  rcu_periph_clock_enable(RCU_TIMER18);
#endif
#if TIM19_ENABLE
  rcu_periph_clock_enable(RCU_TIMER19);
#endif
#if TIM20_ENABLE
  rcu_periph_clock_enable(RCU_TIMER20);
#endif
  
  //UART power control
#if UART0_ENABLE
  rcu_periph_clock_enable(RCU_USART0);
#endif
#if UART1_ENABLE
  rcu_periph_clock_enable(RCU_USART1);
#endif
#if UART2_ENABLE
  rcu_periph_clock_enable(RCU_USART2);
#endif
#if UART3_ENABLE
  rcu_periph_clock_enable(RCU_UART3);
#endif
#if UART4_ENABLE
  rcu_periph_clock_enable(RCU_UART4);
#endif

  
  //SPI power control
#if SPI0_ENABLE
  rcu_periph_clock_enable(RCU_SPI0);
#endif
#if SPI1_ENABLE
  rcu_periph_clock_enable(RCU_SPI1);
#endif
#if SPI2_ENABLE
  rcu_periph_clock_enable(RCU_SPI2);
#endif
}

/*
*/
void HAL_System::close_all_periph()
{
  enter_critical();
  
  for(int i = 0; i < 8; i++) 
  {
    NVIC->ICER[i] = 0xFFFFFFFF; // Disable interrupt
    NVIC->ICPR[i] = 0xFFFFFFFF; // Clear interrupt flag
  }
  rcu_deinit();
}
/*
*/
void HAL_System::reset()
{
  close_all_periph();
  NVIC_SystemReset();
}
/*
*/
void HAL_System::fwdgt_config(uint16_t counter, uint16_t psc_div)
{
  /* enable IRC40K */
  rcu_osci_on(RCU_IRC40K);
    
  /* wait till IRC40K is ready */
  while(SUCCESS != rcu_osci_stab_wait(RCU_IRC40K)){
  }
  
  fwdgt_config(counter, psc_div);
  fwdgt_enable();
}
/*
*/
void HAL_System::wwdgt_config(uint16_t counter, uint16_t window, uint16_t psc_div)
{
  /* enable WWDGT clock */
  rcu_periph_clock_enable(RCU_WWDGT);
  wwdgt_config(counter, window, psc_div);
  wwdgt_enable();
}
/*
*/
void HAL_System::fwdgt_feed()
{
  fwdgt_counter_reload();
}
/*
*/
void HAL_System::wwdgt_feed(uint16_t counter)
{
  wwdgt_counter_update(counter);
}




